package com.jxLC.Action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.simple.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ImgUploadAction extends ActionSupport {

	private File imgFile;
	private String imgWidth;
	private String imgHeight;
	private String align;
	private String  imgTitle;

	public String getImgTitle() {
		return imgTitle;
	}

	public void setImgTitle(String imgTitle) {
		this.imgTitle = imgTitle;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgWidth() {
		return imgWidth;
	}

	public void setImgWidth(String imgWidth) {
		this.imgWidth = imgWidth;
	}

	public String getImgHeight() {
		return imgHeight;
	}

	public void setImgHeight(String imgHeight) {
		this.imgHeight = imgHeight;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	/**
	 * 68. * 上传图片 69. * @return 70.
	 */
	public String uploadImage() {
		HttpServletRequest request;
		HttpServletResponse response = ServletActionContext.getResponse();
		request = ServletActionContext.getRequest();
		String imageName = "_topic_" + new Date().getTime() + Math.random()
				* 10000 + ".jpg";
		File dirPath = new File(ServletActionContext.getRequest().getSession()
				.getServletContext().getRealPath("/")
				+ "\\upload\\");
		// 如果文件夹不存在，创建它
		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}
		// 文件存放路径
		String newPath = dirPath + "\\" + imageName;
		// 上传文件
		try {
			copy(imgFile, newPath);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		String url = "http://" + request.getServerName() + ":"
				+ request.getServerPort() + request.getContextPath()
				+ "/upload/" + imageName;

		JSONObject obj = new JSONObject();
		obj.put("error", 0);
		obj.put("url", url);
		System.out.println(obj.toJSONString());
		PrintWriter out =null;
		try {
			out = response.getWriter();
			out.println(obj.toJSONString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		//out.write(result);
		out.close();
		return null;
	}

	/**
	 * 119. * 拷贝文件 120. * 121. * @param upload文件流 122. * @param newPath新文件路径和名称
	 * 123. * @throws Exception
	 */
	private void copy(File upload, String newPath) throws Exception {
		FileOutputStream fos = new FileOutputStream(newPath);
		FileInputStream fis = new FileInputStream(upload);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}
		fos.close();
		fis.close();
	}

	/**
	 * 138. * Ajax辅助方法 获取 PrintWriter 139. * @return 140. * @throws IOException
	 * 141. * @throws IOException 142. * request.setCharacterEncoding("utf-8");
	 * 143. response.setContentType("text/html; charset=utf-8"); 144.
	 */
	private PrintWriter encodehead(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		return response.getWriter();
	}

}
